﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using CardLogistics.Model.CardArrival;

namespace CardLogistics.DAL.CardArrival
{
    /// <summary>
    /// 白卡入库明细_数据访问类
    /// </summary>
    public class Storage_Detailed_CardData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "Storage_Detailed_Card", StrFields, "CardNo", 1);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }


        /// <summary>
        /// 根据验收入库外键查询入库明细
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoByCheckId(string CheckId)
        {
            return GetList(" CheckId= '" + CheckId + "' ", "*");
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Storage_Detailed_Card GetModel(string rId)
        {
            Storage_Detailed_Card model = new Storage_Detailed_Card();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["BOXNO"] != null)
                {
                    model.BOXNO = row["BOXNO"].ToString();
                }
                if (row["STARTNO"] != null)
                {
                    model.STARTNO = row["STARTNO"].ToString();
                }
                if (row["ENDNO"] != null)
                {
                    model.ENDNO = row["ENDNO"].ToString();
                }
                if (row["CARDNO"] != null)
                {
                    model.CARDNO = row["CARDNO"].ToString();
                }
                if (row["DESERVEDNUMBER"] != null && row["DESERVEDNUMBER"].ToString() != "")
                {
                    model.DESERVEDNUMBER = decimal.Parse(row["DESERVEDNUMBER"].ToString());
                }
                if (row["ACTUALNUMBER"] != null && row["ACTUALNUMBER"].ToString() != "")
                {
                    model.ACTUALNUMBER = decimal.Parse(row["ACTUALNUMBER"].ToString());
                }
                if (row["MISSINGCARDNO"] != null)
                {
                    model.MISSINGCARDNO = row["MISSINGCARDNO"].ToString();
                }
                if (row["CHECKID"] != null)
                {
                    model.CHECKID = row["CHECKID"].ToString();
                }
            }
            return model;
        }


        /// <summary>
        /// 卡片退货 根据合同批次明细ID查询该合同的入库盒号信息
        /// </summary>
        /// <param name="ID">合同批次明细ID</param>
        /// <returns></returns>
        public DataTable GetInInfoByConID(string ID)
        {
            StringBuilder sbWh = new StringBuilder(" conid='" + ID + "' ");
            sbWh.Append(" and issubmit=5 ");//只获取已经审批通过的入库记录
            //需要过滤掉已经发放的盒号信息
            sbWh.Append(" and (");
            sbWh.Append(" cardno not in(select cardno from v_putoutsendcard_detail where issubmit=5)");
            sbWh.Append(" or cardno in(");
            //--调拨入库没有调拨出库
            sbWh.Append(" select cardno from putinallocation_detail where allocationid not in(select PUTINALLOCATIONID from putoutallocation) ");
            sbWh.Append(" ))");
            return CommonData.GetListOrderBy(sbWh.ToString(), "v_storage_check_card_detailed", "*", "startno", 1);
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(Storage_Detailed_Card model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
					new OracleParameter("vBOXNO", OracleType.VarChar,50),
					new OracleParameter("vSTARTNO", OracleType.Number,4),
					new OracleParameter("vENDNO", OracleType.Number,4),
					new OracleParameter("vCARDNO", OracleType.VarChar,50),
					new OracleParameter("vDESERVEDNUMBER", OracleType.Number,4),
					new OracleParameter("vACTUALNUMBER", OracleType.Number,4),
					new OracleParameter("vMISSINGCARDNO", OracleType.NVarChar),
					new OracleParameter("vCHECKID", OracleType.VarChar,50)};

                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.BOXNO;
                parameters[2].Value = model.STARTNO;
                parameters[3].Value = model.ENDNO;
                parameters[4].Value = model.CARDNO;
                parameters[5].Value = model.DESERVEDNUMBER;
                parameters[6].Value = model.ACTUALNUMBER;
                parameters[7].Value = model.MISSINGCARDNO;
                parameters[8].Value = model.CHECKID;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Storage_Detailed_Card_Add", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion
        
        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(Storage_Detailed_Card model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,50),
					new OracleParameter("vBOXNO", OracleType.VarChar,50),
					new OracleParameter("vSTARTNO", OracleType.Number,4),
					new OracleParameter("vENDNO", OracleType.Number,4),
					new OracleParameter("vCARDNO", OracleType.VarChar,50),
					new OracleParameter("vDESERVEDNUMBER", OracleType.Number,4),
					new OracleParameter("vACTUALNUMBER", OracleType.Number,4),
					new OracleParameter("vMISSINGCARDNO", OracleType.NVarChar),
					new OracleParameter("vCHECKID", OracleType.VarChar,50),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.BOXNO;
                parameters[2].Value = model.STARTNO;
                parameters[3].Value = model.ENDNO;
                parameters[4].Value = model.CARDNO;
                parameters[5].Value = model.DESERVEDNUMBER;
                parameters[6].Value = model.ACTUALNUMBER;
                parameters[7].Value = model.MISSINGCARDNO;
                parameters[8].Value = model.CHECKID;
                parameters[9].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Storage_Detailed_Card_Update", parameters, parameters[9]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Storage_Detailed_Card_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
